package com.np.xxpermissions1.permission;

import android.app.Activity;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.hjq.permissions.IPermissionInterceptor;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.PermissionFragment;
import com.hjq.permissions.XXPermissions;
import com.np.xxpermissions1.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限管理
 */
public class PermissionManager {

    public interface OnPermissionFinishListener {
        void onFinished();
    }

    //***flag***{ 超声主界面中检查并申请权限
    public static void doPermission(FragmentActivity activity, OnPermissionFinishListener listener) {
        //***flag***{ 超声主界面中检查并申请权限
        if (XXPermissions.isGranted(activity, Permission.MANAGE_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE, Permission.WRITE_SETTINGS, Permission.CAMERA, Permission.RECORD_AUDIO)) {
            if (listener != null) listener.onFinished();
        } else {
            if (Utils.SharedPreferences.getValue("permission_already_set", false)) {
                if (Utils.SharedPreferences.getValue("permission_no_remind", false)) {
                    if (listener != null) listener.onFinished();
                } else {
                    DialogPermissionMultiple.show(activity, type -> {
                        switch (type) {
                            case NoRemind:
                                Utils.SharedPreferences.setValue("permission_no_remind", true);
                                if (listener != null) listener.onFinished();
                                break;
                            case ManualGrant:
                                XXPermissions.startPermissionActivity(activity, Permission.MANAGE_EXTERNAL_STORAGE, Permission.WRITE_SETTINGS);
                                if (listener != null) listener.onFinished();
                                break;
                        }
                    });
                }
            } else {
                XXPermissions xxPerm = XXPermissions.with(activity);
                //delete by np
//                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
//                    // 请求外部存储权限(Android 11及以上)
//                    xxPerm.permission(Permission.WRITE_EXTERNAL_STORAGE);
//                } else {
//                    xxPerm.permission(Permission.MANAGE_EXTERNAL_STORAGE);
//                }
                xxPerm.permission(Permission.MANAGE_EXTERNAL_STORAGE);
                xxPerm.permission(Permission.CAMERA);
                xxPerm
                        // 设置拦截器
                        .interceptor(new IPermissionInterceptor() {
                            @Override
                            public void launchPermissionRequest(@NonNull Activity activity, @NonNull List<String> allPermissions, @Nullable OnPermissionCallback callback) {
                                DialogPermission.show((FragmentActivity) activity, type -> {
                                    switch (type) {
                                        case NextTime:
                                            if (listener != null) listener.onFinished();
                                            break;
                                        case Grant:
                                            PermissionFragment.launch(activity, new ArrayList<>(allPermissions), this, callback);
                                            break;
                                    }
                                });
                            }
                        })
                        // 请求外部存储权限(Android 11及以上)
                        //.permission(Permission.MANAGE_EXTERNAL_STORAGE)
                        // 如果申请了MANAGE_EXTERNAL_STORAGE,不需要再申请WRITE_EXTERNAL_STORAGE
                        //.permission(Permission.WRITE_EXTERNAL_STORAGE)
                        // 请求系统设置权限(Android 6.0及以上)
                        .permission(Permission.WRITE_SETTINGS)
                        // 请求权限
                        .request(new OnPermissionCallback() {
                            @Override
                            public void onGranted(List<String> permissions, boolean all) {
                                if (all) {
                                    if (listener != null) listener.onFinished();
                                }
                            }

                            @Override
                            public void onDenied(List<String> permissions, boolean never) {
                                DialogPermissionMultiple.show(activity, type -> {
                                    switch (type) {
                                        case NoRemind:
                                            Utils.SharedPreferences.setValue("permission_no_remind", true);
                                            if (listener != null) listener.onFinished();
                                            break;
                                        case ManualGrant:
                                            XXPermissions.startPermissionActivity(activity, permissions);
                                            if (listener != null) listener.onFinished();
                                            break;
                                    }
                                });
                                Utils.SharedPreferences.setValue("permission_already_set", true);
                            }
                        });
            }
        }
    }
    //***flag***}

    //***flag***{ 检查外部存储权限
    public static boolean isStorageEnable(FragmentActivity activity, boolean remind) {
        boolean ok = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            if (!XXPermissions.isGranted(activity, Permission.MANAGE_EXTERNAL_STORAGE)) {
                if (remind) {
                    DialogPermissionSingle.show(activity, type -> {
                        if (type == DialogPermissionSingle.Type.ManualGrant) {
                            XXPermissions.startPermissionActivity(activity, Permission.MANAGE_EXTERNAL_STORAGE, Permission.WRITE_SETTINGS);
                        }
                    }, Permission.MANAGE_EXTERNAL_STORAGE);
                }
                ok = false;
            }
        } else {
            if (!XXPermissions.isGranted(activity, Permission.WRITE_EXTERNAL_STORAGE)) {
                if (remind) {
                    DialogPermissionSingle.show(activity, type -> {
                        if (type == DialogPermissionSingle.Type.ManualGrant) {
                            XXPermissions.startPermissionActivity(activity, Permission.WRITE_EXTERNAL_STORAGE);
                        }
                    }, Permission.WRITE_EXTERNAL_STORAGE);
                }
                ok = false;
            }
        }
        return ok;
    }
    //***flag***}
}
